
import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';

import '../../common/button_action.dart';
import '../../gen/colors.gen.dart';

typedef OnItemSelected = void Function(SheetItem item);

class ItemSheet extends StatelessWidget {
  //标题，可选
  final Widget? title;

  //选项
  final List<SheetItem> items;

  //选择回调
  final OnItemSelected onItemSelected;

  //取消按钮，可选
  final ButtonAction? negative;

  const ItemSheet({Key? key,
    this.title,
    required this.items,
    required this.onItemSelected,
    this.negative});

  ItemSheet.withCancel({Key? key,
    this.title,
    required this.items,
    required this.onItemSelected,
    required VoidCallback onCancel})
      : negative = ButtonAction(
      onClick: onCancel,
      text: '取消',
      textStyle:
      const TextStyle(color: ColorName.textPrimary, fontSize: 15));

  @override
  Widget build(BuildContext context) {
    final children = <Widget>[_buildItems()];
    if (title != null){
      children.insert(0, _buildTitle());
    }
    if(negative != null){
      children.addAll([
        Container(height: 7.5, color: ColorName.pageBg),
        _buildNegative(),
      ]);
    }

    return Container(
      decoration: const BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(21),
          topRight: Radius.circular(21),
        ),
        color: Colors.white,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: children,
      ),
    );
  }

  Widget _buildNegative() {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => negative!.onClick.call(),
      child: Container(
        alignment: Alignment.center,
        padding: const EdgeInsets.symmetric(vertical: 15),
        margin: const EdgeInsets.only(bottom: 10),
        child: Text(negative!.text ?? '取消', style: negative!.textStyle),
      ),
    );
  }

  Widget _buildTitle() {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 9),
      child: DefaultTextStyle(
        style: const TextStyle(
          fontSize: 13.5,
          color: ColorName.textSub,
          fontWeight: FontWeight.bold,
        ),
        child: title!,
      ),
    );
  }

  Widget _buildItems() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: items.length,
        itemBuilder: (_, index) {
          final item = items[index];
          return GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              Get.back();
              onItemSelected.call(item);
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(
                  vertical: item.subText == null ? 14 : 10.5),
              child: Column(
                children: [
                  Text(item.text, style: item.style,),
                  if (item.subText != null)
                    Container(
                      margin: const EdgeInsets.only(top: 3),
                      child: Text(item.subText!, style: item.subStyle),
                    )
                ],
              ),
            ),
          );
        },
        separatorBuilder: (_, __) =>
        const Divider(color: ColorName.pageBg, height: 1, thickness: 1,));
  }
}

class SheetItem {
  dynamic value;
  String text;
  String? subText;
  TextStyle style;
  TextStyle subStyle;

  SheetItem({
    required this.value,
    required this.text,
    this.subText,
    this.style = const TextStyle(color: ColorName.textPrimary, fontSize: 15),
    this.subStyle = const TextStyle(color: ColorName.textHint, fontSize: 12),
  });
}
